
#include "CoordConvert.h"

#include <math.h>


void POLAR2D_To_POINT2D(POLAR2D_PTR polar, POINT2D_PTR rect)
{
	// convert polar to rectangular
	rect->x = polar->r*cosf(polar->theta);
	rect->y = polar->r*sinf(polar->theta);

} // end POLAR2D_To_POINT2D

////////////////////////////////////////////////////////////////

void POLAR2D_To_RectXY(POLAR2D_PTR polar, float *x, float *y)
{
	// convert polar to rectangular
	*x = polar->r*cosf(polar->theta);
	*y = polar->r*sinf(polar->theta);

} // end POLAR2D_To_RectXY

///////////////////////////////////////////////////////////////

void POINT2D_To_POLAR2D(POINT2D_PTR rect, POLAR2D_PTR polar)
{
	// convert rectangular to polar
	polar->r     = sqrtf((rect->x * rect->x) + (rect->y * rect->y));
	polar->theta = atanf(rect->y/rect->x);

} // end POINT2D_To_POLAR2D

////////////////////////////////////////////////////////////

void POINT2D_To_PolarRTh(POINT2D_PTR rect, float *r, float *theta)
{
	// convert rectangular to polar
	*r=sqrtf((rect->x * rect->x) + (rect->y * rect->y));
	*theta = atanf(rect->y/rect->x);

} // end POINT2D_To_PolarRTh

///////////////////////////////////////////////////////////////

void CYLINDRICAL3D_To_POINT3D(CYLINDRICAL3D_PTR cyl, POINT3D_PTR rect)
{
	// convert cylindrical to rectangular
	rect->x = cyl->r*cosf(cyl->theta);
	rect->y = cyl->r*sinf(cyl->theta);
	rect->z = cyl->z;

} // end CYLINDRICAL3D_To_POINT3D

////////////////////////////////////////////////////////////////

void CYLINDRICAL3D_To_RectXYZ(CYLINDRICAL3D_PTR cyl,  
							  float *x, float *y, float *z)
{
	// convert cylindrical to rectangular
	*x = cyl->r*cosf(cyl->theta);
	*y = cyl->r*sinf(cyl->theta);
	*z = cyl->z;

} // end CYLINDRICAL3D_To_RectXYZ

///////////////////////////////////////////////////////////////

void POINT3D_To_CYLINDRICAL3D(POINT3D_PTR rect, 
							  CYLINDRICAL3D_PTR cyl)
{
	// convert rectangular to cylindrical
	cyl->r     = sqrtf((rect->x * rect->x) + (rect->y * rect->y));
	cyl->theta = atanf(rect->y/rect->x);
	cyl->z     = rect->z;

} // end POINT3D_To_CYLINDRICAL3D

///////////////////////////////////////////////////////////////

void POINT3D_To_CylindricalRThZ(POINT3D_PTR rect, 
								float *r, float *theta, float *z)
{
	// convert rectangular to cylindrical
	*r     = sqrtf((rect->x * rect->x) + (rect->y * rect->y));
	*theta = atanf(rect->y/rect->x);
	*z     = rect->z;

} // end POINT3D_To_CylindricalRThZ

///////////////////////////////////////////////////////////////

void SPHERICAL3D_To_POINT3D(SPHERICAL3D_PTR sph, POINT3D_PTR rect)
{
	// convert spherical to rectangular
	float r;

	// pre-compute r, and z
	r       = sph->p*sinf(sph->phi);
	rect->z = sph->p*cosf(sph->phi);

	// use r to simplify computation of x,y
	rect->x = r*cosf(sph->theta);
	rect->y = r*sinf(sph->theta);

} // end SPHERICAL3D_To_POINT3D

////////////////////////////////////////////////////////////////

void SPHERICAL3D_To_RectXYZ(SPHERICAL3D_PTR sph, 
							float *x, float *y, float *z)
{
	// convert spherical to rectangular

	float r;

	// pre-compute r, and z
	r  = sph->p*sinf(sph->phi);
	*z = sph->p*cosf(sph->phi);

	// use r to simplify computation of x,y
	*x = r*cosf(sph->theta);
	*y = r*sinf(sph->theta);

} // end SPHERICAL3D_To_RectXYZ

///////////////////////////////////////////////////////////

void POINT3D_To_SPHERICAL3D(POINT3D_PTR rect, SPHERICAL3D_PTR sph)
{
	// convert rectangular to spherical
	sph->p = sqrtf((rect->x*rect->x)+(rect->y*rect->y)+(rect->z*rect->z));

	sph->theta = atanf(rect->y/rect->x);

	// we need r to compute phi
	float r = sph->p*sinf(sph->phi);

	sph->phi   = asinf(r/sph->p);

} // end POINT3D_To_CYLINDRICAL3D

////////////////////////////////////////////////////////////

void POINT3D_To_SphericalPThPh(POINT3D_PTR rect, 
							   float *p, float *theta, float *phi)
{
	// convert rectangular to spherical
	*p     = sqrtf((rect->x*rect->x)+(rect->y*rect->y)+(rect->z*rect->z));
	*theta = atanf(rect->y/rect->x);

	// we need r to compute phi
	float r = sqrtf((rect->x * rect->x) + (rect->y * rect->y));
	*phi    = asinf(r / (*p));

} // end POINT3D_To_SphericalPThPh